﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.facebook.session {
    import com.facebook.*;
    import com.facebook.events.*;
    import com.facebook.delegates.*;
    import com.facebook.net.*;
    import flash.net.*;
    import com.facebook.data.*;
    import com.facebook.errors.*;
    import com.facebook.commands.users.*;
    import com.facebook.data.auth.*;
    import com.facebook.commands.auth.*;

    public class DesktopSession extends WebSession implements IFacebookSession {

        protected var _auth_token:String;
        protected var loginRequest:IFacebookCallDelegate;
        protected var _waiting_for_login:Boolean = false;
        protected var _offline_access:Boolean = false;

        public function DesktopSession(_arg1:String, _arg2:String=null, _arg3:String=null){
            super(_arg1, null);
            this._is_connected = false;
            this._secret = _arg2;
            if (_arg3){
                this._session_key = _arg3;
            };
        }
        override public function post(_arg1:FacebookCall):IFacebookCallDelegate{
            rest_url = REST_URL;
            if ((_arg1 is IUploadPhoto)){
                return (new WebImageUploadDelegate(_arg1, this));
            };
            if ((_arg1 is IUploadVideo)){
                rest_url = VIDEO_URL;
                return (new VideoUploadDelegate(_arg1, this));
            };
            return (new DesktopDelegate(_arg1, this));
        }
        override public function get waiting_for_login():Boolean{
            return (this._waiting_for_login);
        }
        protected function onLogin(_arg1:FacebookEvent):void{
            var _local2:URLRequest;
            var _local3:String;
            _arg1.target.removeEventListener(FacebookEvent.COMPLETE, this.onLogin);
            if (_arg1.success){
                this._auth_token = (_arg1.data as StringResultData).value;
                _local2 = new URLRequest();
                _local3 = "?";
                if (this._offline_access){
                    _local3 = (_local3 + "ext_perm=offline_access&");
                };
                _local2.url = (((((((login_url + _local3) + "api_key=") + api_key) + "&v=") + api_version) + "&auth_token=") + this._auth_token);
                navigateToURL(_local2, "_blank");
                this._waiting_for_login = true;
                dispatchEvent(new FacebookEvent(FacebookEvent.WAITING_FOR_LOGIN));
            } else {
                this.onConnectionError(_arg1.error);
            };
        }
        protected function onConnectionError(_arg1:FacebookError):void{
            _is_connected = false;
            dispatchEvent(new FacebookEvent(FacebookEvent.CONNECT, false, false, false, null, _arg1));
        }
        protected function tokenCreated():void{
            navigateToURL(new URLRequest(login_url));
        }
        override public function login(_arg1:Boolean):void{
            this._offline_access = _arg1;
            _session_key = null;
            var _local2:FacebookCall = new CreateToken();
            _local2.session = this;
            var _local3 = _local2;
            _local3.facebook_internal::initialize();
            _local2.addEventListener(FacebookEvent.COMPLETE, this.onLogin);
            this.post(_local2);
        }
        protected function onVerifyLogin(_arg1:FacebookEvent):void{
            var _local2:FacebookEvent = new FacebookEvent(FacebookEvent.CONNECT);
            _local2.success = _arg1.success;
            if (_arg1.success){
                facebook_internal::_uid = (_arg1.data as StringResultData).value;
                _local2.data = _arg1.data;
                _is_connected = true;
            } else {
                _local2.error = _arg1.error;
                _is_connected = false;
            };
            dispatchEvent(_local2);
        }
        override public function verifySession():void{
            var _local1:FacebookCall;
            if (_session_key){
                _local1 = new GetLoggedInUser();
                _local1.session = this;
                var _local2 = _local1;
                _local2.facebook_internal::initialize();
                _local1.addEventListener(FacebookEvent.COMPLETE, this.onVerifyLogin, false, 0, true);
                this.post(_local1);
                dispatchEvent(new FacebookEvent(FacebookEvent.VERIFYING_SESSION));
            } else {
                _is_connected = false;
                dispatchEvent(new FacebookEvent(FacebookEvent.CONNECT));
            };
        }
        protected function validateSessionReply(_arg1:FacebookEvent):void{
            var _local2:GetSessionData;
            if (_arg1.success){
                _local2 = (_arg1.data as GetSessionData);
                facebook_internal::_uid = _local2.uid;
                this._session_key = _local2.session_key;
                this._expires = _local2.expires;
                this._secret = ((((_local2.secret == null)) || ((_local2.secret == "")))) ? this._secret : _local2.secret;
                _is_connected = true;
                dispatchEvent(new FacebookEvent(FacebookEvent.CONNECT, false, false, true, _local2));
            } else {
                this.onConnectionError(_arg1.error);
            };
        }
        override public function refreshSession():void{
            this._waiting_for_login = false;
            var _local1:GetSession = new GetSession(this._auth_token);
            _local1.session = this;
            var _local2 = _local1;
            _local2.facebook_internal::initialize();
            _local1.addEventListener(FacebookEvent.COMPLETE, this.validateSessionReply);
            this.post(_local1);
        }

    }
}//package com.facebook.session 
